package com.jeff.android.lab.module.widget.webview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;

import butterknife.BindView;

/**
 * @author: dingfeng
 * @date: 2019-09-20
 * @since: 1.0.0
 **/
public class WebViewFragment extends BaseFragment {

    @BindView(R.id.webview)
    WebView mWebView;

    public static WebViewFragment get(String url) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("data", url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getRootLayout() {
        return R.layout.webview_fragment;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setSavePassword(false);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setDomStorageEnabled(true);

        mWebView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
        mWebView.getSettings().setAllowFileAccess(true);
        //如果访问的页面中有Javascript，则webview必须设置支持Javascript
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setDatabaseEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDisplayZoomControls(false);

        // 自适应屏幕
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
    }


    @Override
    protected void initData() {
//        mWebView.loadUrl("https://h5.iyunbao.com/m/my/order/orderDetail?orderId=h7KQCUrA2Gwzo9ExtJsWcg");

//        mWebView.loadUrl("https://h5.iyunbao.com/m/my/order/policydetailnew?policyNo=LmLUZjOq9IYjEkpvwQ-WavRzIY66jA46wuEes08w3i8");

//        mWebView.loadUrl("https://www.baidu.com/");

        if (getArguments() != null) {
            String url = getArguments().getString("data");
            mWebView.loadUrl(url);
        }
    }

}
